﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsTemplate
    {
        public int Add(KeRui.Model.CmsTemplate model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@TemplateName", SqlDbType.NVarChar, 50), new SqlParameter("@TemplateFolder", SqlDbType.VarChar, 50), new SqlParameter("@TemplateDir", SqlDbType.VarChar, 500), new SqlParameter("@ServerID", SqlDbType.Int, 4) };
            parameters[0].Value = model.TemplateName;
            parameters[1].Value = model.TemplateFolder;
            parameters[2].Value = model.TemplateDir;
            parameters[3].Value = model.ServerID;
            return DbHelperSQL.RunProcedure("CmsTemplate_ADD", parameters, out num);
        }

        public int Delete(int TemplateID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@TemplateID", SqlDbType.Int, 4) };
            parameters[0].Value = TemplateID;
            return DbHelperSQL.RunProcedure("CmsTemplate_Delete", parameters, out num);
        }

        public KeRui.Model.CmsTemplate GetModel(int TemplateID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@TemplateID", SqlDbType.Int, 4) };
            parameters[0].Value = TemplateID;
            KeRui.Model.CmsTemplate template = new KeRui.Model.CmsTemplate();
            DataSet set = DbHelperSQL.RunProcedure("CmsTemplate_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["TemplateID"].ToString() != "")
                {
                    template.TemplateID = int.Parse(set.Tables[0].Rows[0]["TemplateID"].ToString());
                }
                template.TemplateName = set.Tables[0].Rows[0]["TemplateName"].ToString();
                template.TemplateFolder = set.Tables[0].Rows[0]["TemplateFolder"].ToString();
                template.TemplateDir = set.Tables[0].Rows[0]["TemplateDir"].ToString();
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    template.IsUsing = int.Parse(set.Tables[0].Rows[0]["IsUsing"].ToString());
                }
                if (set.Tables[0].Rows[0]["SiteNum"].ToString() != "")
                {
                    template.SiteNum = int.Parse(set.Tables[0].Rows[0]["SiteNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["ServerID"].ToString() != "")
                {
                    template.ServerID = int.Parse(set.Tables[0].Rows[0]["ServerID"].ToString());
                }
                template.ServerName = set.Tables[0].Rows[0]["ServerName"].ToString();
                template.ServerIP = set.Tables[0].Rows[0]["ServerIP"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    template.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return template;
            }
            return null;
        }

        public KeRui.Model.CmsTemplate GetModel(int ServerID, string TemplateFolder)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ServerID", SqlDbType.Int, 4), new SqlParameter("@TemplateFolder", SqlDbType.VarChar) };
            parameters[0].Value = ServerID;
            parameters[1].Value = TemplateFolder;
            KeRui.Model.CmsTemplate template = new KeRui.Model.CmsTemplate();
            DataSet set = DbHelperSQL.RunProcedure("CmsTemplate_GetModelByCondition", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["TemplateID"].ToString() != "")
                {
                    template.TemplateID = int.Parse(set.Tables[0].Rows[0]["TemplateID"].ToString());
                }
                template.TemplateName = set.Tables[0].Rows[0]["TemplateName"].ToString();
                template.TemplateFolder = set.Tables[0].Rows[0]["TemplateFolder"].ToString();
                template.TemplateDir = set.Tables[0].Rows[0]["TemplateDir"].ToString();
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    template.IsUsing = int.Parse(set.Tables[0].Rows[0]["IsUsing"].ToString());
                }
                if (set.Tables[0].Rows[0]["SiteNum"].ToString() != "")
                {
                    template.SiteNum = int.Parse(set.Tables[0].Rows[0]["SiteNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["ServerID"].ToString() != "")
                {
                    template.ServerID = int.Parse(set.Tables[0].Rows[0]["ServerID"].ToString());
                }
                template.ServerName = set.Tables[0].Rows[0]["ServerName"].ToString();
                template.ServerIP = set.Tables[0].Rows[0]["ServerIP"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    template.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return template;
            }
            return null;
        }

        public int Update(KeRui.Model.CmsTemplate model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@TemplateID", SqlDbType.Int, 4), new SqlParameter("@TemplateName", SqlDbType.NVarChar, 50), new SqlParameter("@TemplateFolder", SqlDbType.VarChar, 50), new SqlParameter("@TemplateDir", SqlDbType.VarChar, 500), new SqlParameter("@ServerID", SqlDbType.Int, 4) };
            parameters[0].Value = model.TemplateID;
            parameters[1].Value = model.TemplateName;
            parameters[2].Value = model.TemplateFolder;
            parameters[3].Value = model.TemplateDir;
            parameters[4].Value = model.ServerID;
            return DbHelperSQL.RunProcedure("CmsTemplate_Update", parameters, out num);
        }
    }
}

